package com.ktjy.ktmall.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ktjy.ktmall.domain.Attr;
import com.ktjy.ktmall.domain.SkuInfo;
import com.ktjy.ktmall.service.IAttrService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ktjy.common.annotation.Log;
import com.ktjy.common.core.controller.BaseController;
import com.ktjy.common.core.domain.AjaxResult;
import com.ktjy.common.enums.BusinessType;
import com.ktjy.ktmall.domain.SkuAttrValues;
import com.ktjy.ktmall.service.ISkuAttrValuesService;
import com.ktjy.common.utils.poi.ExcelUtil;
import com.ktjy.common.core.page.TableDataInfo;

/**
 * SKU关联属性Controller
 *
 * @author ktjy
 * @date 2025-03-19
 */
@RestController
@RequestMapping("/ktmall/skuAttrValues")
public class SkuAttrValuesController extends BaseController
{
    @Autowired
    private ISkuAttrValuesService skuAttrValuesService;


    @Autowired
    private IAttrService attrService;


    /**
     * 删除sku属性值列表
     */
    @PreAuthorize("@ss.hasPermi('ktmall:skuAttrValues:del')")
    @GetMapping("/del/{stockSku}")
    public void del(@PathVariable("stockSku") String stockSku)
    {
        QueryWrapper<SkuAttrValues> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stock_sku",stockSku);
        skuAttrValuesService.remove(queryWrapper);
    }

    /**
     * 规格属性值列表
     */
//    @PreAuthorize("@ss.hasPermi('ktmall:skuAttrValues:attrValue')")
    @GetMapping("/attrValue")
    public TableDataInfo attrLists(Long attrId,String stockSku) {
        List<Attr> list = attrService.getAttrListByAttrId(attrId,stockSku);
        return getDataTable(list);
    }

    /**
     * 规格属性值列表
     */
//    @PreAuthorize("@ss.hasPermi('ktmall:skuAttrValues:attrList')")
    @GetMapping("/attrList")
    public TableDataInfo attrList(String stockSku) {
        List<Attr> list = skuAttrValuesService.selectSkuAttrValuesListByStockSku(stockSku);
        return getDataTable(list);
    }



/**
 * 查询SKU关联属性列表
 */
@PreAuthorize("@ss.hasPermi('ktmall:skuAttrValues:list')")
@GetMapping("/list")
    public TableDataInfo list(SkuAttrValues skuAttrValues)
    {
        startPage();
        List<SkuAttrValues> list = skuAttrValuesService.selectSkuAttrValuesList(skuAttrValues);
        return getDataTable(list);
    }


    /**
     * 导出SKU关联属性列表
     */
    @PreAuthorize("@ss.hasPermi('ktmall:skuAttrValues:export')")
    @Log(title = "SKU关联属性", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SkuAttrValues skuAttrValues)
    {
        List<SkuAttrValues> list = skuAttrValuesService.list();
        ExcelUtil<SkuAttrValues> util = new ExcelUtil<SkuAttrValues>(SkuAttrValues.class);
        util.exportExcel(response, list, "SKU关联属性数据");
    }

    /**
     * 获取SKU关联属性详细信息
     */
    @PreAuthorize("@ss.hasPermi('ktmall:skuAttrValues:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(skuAttrValuesService.getById(id));
    }

    /**
     * 新增SKU关联属性
     */
    @PreAuthorize("@ss.hasPermi('ktmall:skuAttrValues:add')")
    @Log(title = "SKU关联属性", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SkuAttrValues skuAttrValues)
    {
        return toAjax(skuAttrValuesService.save(skuAttrValues));
    }

    /**
     * 修改SKU关联属性
     */
    @PreAuthorize("@ss.hasPermi('ktmall:skuAttrValues:edit')")
    @Log(title = "SKU关联属性", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SkuAttrValues skuAttrValues)
    {
        return toAjax(skuAttrValuesService.updateById(skuAttrValues));
    }

    /**
     * 删除SKU关联属性
     */
    @PreAuthorize("@ss.hasPermi('ktmall:skuAttrValues:remove')")
    @Log(title = "SKU关联属性", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Integer> ids)
    {
        return toAjax(skuAttrValuesService.removeByIds(ids));
    }
}